<?php

namespace Common\Model;

use Think\Model;
use Think\Model\RelationModel;

class UserModel extends RelationModel
{
    protected $_auto = array(
        array('status', '0', self::MODEL_BOTH),
    );
    protected $_link = array(
        'user_info' => array(
            'mapping_type' => self::HAS_ONE,
            'class_name' => 'UserInfo',
            'foreign_key' => 'user_id',
        ),
    );

    public function update($data, $relation = false)
    {
        if (empty($data['id'])) {
            $result = $this->relation($relation)->add($data);
        } else {
            $result = $this->relation($relation)->save($data);
        }
        return $result;
    }

    public function delete($id)
    {
        $data['status'] = -1;
        $result = $this->where('id=%d', $id)->save($data);
        if ($result) {
            return true;
        }
        return false;
    }

    public function do_login($account, $password)
    {
        $user = $this->where("account='%s' and password='%s' and status >=0", $account, md5_encrypt($password))->find();

        if ($user && $user['status'] == 1) {
            session('user_auth', $user);
            return true;
        }
        return false;
    }

    public function get($id)
    {
        $result = $this->where('id>=%d', $id)->relation(true)->find();
        return $result;
    }

    public function find_account($account)
    {
        $result = $this->where('account="%s" and status>=%d', $account, 0)->find();
        return $result;
    }

    public function update_name($userid, $name)
    {
        $result = $this->where('id=%d', $userid)->setField('name', $name);
        return $result;
    }

    public function update_status($id, $status)
    {
        $data['status'] = $status;
        $result = $this->where('id=%d', $id)->save($data);
        if ($result) {
            return true;
        }
        return false;
    }

    public function update_password($userid, $oldpassword, $password)
    {
        $condition['id'] = array('eq', $userid);
        $condition['password'] = array('eq', fc_md5($oldpassword));
        $condition['state'] = array('eq', 0);
        $row = $this->where($condition)->field('id,phone,name,idcard,qq')->find();
        if ($row) {
            $result = $this->where('id=%d', $userid)->setField('password', fc_md5($password));
            if ($result) {
                return true;
            }
        }
        return false;
    }

    public function get_list($search_key, $status, $rank, $attribute, $type, $page_no = 1, $page_size = 0, $relation = true)
    {
        $condition = $this->assembleCondition($search_key, $status, $rank, $attribute, $type);
        $count = 0;

        if ($page_size > 0) {
            $list = $this->relation($relation)->where($condition)->order('id asc')->limit(($page_no - 1) * $page_size, $page_size)->select();
            $count = $this->where($condition)->count();
        } else {
            $list = $this->relation($relation)->where($condition)->order('id asc')->select();
        }
        return [$list, $count];
    }

    private function assembleCondition($search_key, $status, $rank, $attribute, $type)
    {
        $condition = array();
        if (!empty($search_key)) {
            $condition['account'] = array('like', '%' . $search_key . '%');
        }
        if (!empty($type)) {
            $condition['role'] = array('eq', $type);
        }
        if ($status != "" && $status != null) {
            $condition['status'] = array('eq', $status);
        } else {
            $condition['status'] = array('egt', 0);
        }

        if ($rank != "" && $rank != null) {
            $condition['rank'] = array('eq', $rank);
        }

        if ($attribute != "" && $attribute != null) {
            $condition['attribute'] = array('eq', $attribute);
        }
        return $condition;
    }
}